package GUI;

import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.GroupLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;

import stochastic.*;
public class GUIPane extends JPanel implements Runnable{
	private JTable table;
	public VisualizerPanel vizualizer;
	private SimulatedAnealing algorithm;
	private MyTableModel model;
	
	public GUIPane(SimulatedAnealing alg ) {
		algorithm = alg;
		vizualizer = new VisualizerPanel();
		model = new MyTableModel();
		table = new JTable(model);
		
		
		table.setPreferredScrollableViewportSize(new Dimension(100, 70));
        table.setFillsViewportHeight(true);

		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.getSelectionModel().addListSelectionListener(
			new ListSelectionListener() {
				
				@Override
				public void valueChanged(ListSelectionEvent event) {
					 int row = table.getSelectedRow();
					 MyTableModel model  = (MyTableModel)table.getModel();
					 Solution s = model.pool.get(row).getSolution();
					 vizualizer.setSolution(s); 
				}
			}
		);
		
		TableColumn column = null;
		for (int i = 0; i < model.getColumnCount(); i++) {
		    column = table.getColumnModel().getColumn(i);
		    if (i == 0) {
		        column.setPreferredWidth(10); //third column is bigger
		    } 
		}
		
		JScrollPane scrollPane = new JScrollPane(table);
		
		
		GroupLayout layout = new GroupLayout(this);
		this.setLayout(layout);
		layout.setAutoCreateGaps(true);
	    layout.setAutoCreateContainerGaps(true);
	    
	    layout.setHorizontalGroup(layout.createSequentialGroup()
    			.addComponent(scrollPane)
	    		.addComponent(vizualizer)
	    	);
	    
	    layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				.addComponent(scrollPane)
	    		.addComponent(vizualizer)
			);
		
		Thread a = new Thread(this);
		
		a.start();
	}
		
	@Override
	public void run() {
		while(!algorithm.isEnd())
		{
			AnnealingState a = new AnnealingState(algorithm.getSolution(), algorithm.getTemperature());
			model.addRow(a);
			algorithm.next();
			
			try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	class MyTableModel extends AbstractTableModel  {

		private String[] columnNames =  {"ID","T", "Cost Function" };
		private ArrayList<AnnealingState> pool;
		
		public MyTableModel() {
			pool = new ArrayList<AnnealingState>();
			
		}
		public void addRow(AnnealingState a) {
			pool.add(0, a);
			fireTableRowsInserted(0, 0);
		}
		@Override
		public int getColumnCount() {
			return columnNames.length;
		}

		@Override
		public int getRowCount() {
			return pool.size();
		}

		@Override
		public Object getValueAt(int row, int col) {
			AnnealingState obj = pool.get(row);
			
			switch (col) {
			case 0:
				return (Object)obj.getID();
			case 1:
				return (Object)obj.getTemperature();
			case 2:
				return (Object)obj.getSolution().getCost();

			default:
				return null;
			}
			
		}
		
		@Override
		public String getColumnName(int col) {
	        return columnNames[col];
	    }
		@Override
		public boolean isCellEditable(int row, int col) {
			return false;
		}
	}
	
	
}
